using UnityEngine;
using LifeChess.Entity;
using UnityEngine.UI;
using SStateMachine;
using LifeChess.GamePlay.Test;

namespace LifeChess.GamePlay
{
    enum Round
    {
        None,
        Player1,
        Player2,
        Conclusion,
    }

    enum RoundTrigger
    {
        None,
        PlayerEnd,
        EndConclusion,
    }

    public class BoardManager : MonoBehaviour
    {
        public GameObject unitBoardPrefabs;
        public Image currentTeamIamge;
        public ChessLifeInfoPanel infoPanel;
        
        public Vector2Int boardSize;

        private BaseChessBoard chessBoard;
        private GameObject boardGameObject;

        private StateMachine<Round, RoundTrigger> rounds;

        public void GenerateChessBoard(Vector2Int size)
        {
            OldGenerateChessBoard(size);

            rounds = new StateMachine<Round, RoundTrigger>(Round.None);

            rounds.Config(Round.None).AddTrigger(RoundTrigger.None, Round.Conclusion);

            // First round is update the life of chess that is on the board
            rounds.Config(Round.Conclusion)
                .AddTrigger(RoundTrigger.EndConclusion, Round.Player1)
                .OnEnter(() =>
                {
                    Debug.Log("Update life round...");
                    chessBoard.UpdateChessState();
                    rounds.Trigger(RoundTrigger.EndConclusion);
                });

            // Second set the player1 who can put chess to be current player
            rounds.Config(Round.Player1)
                .AddTrigger(RoundTrigger.PlayerEnd, Round.Player2)
                .OnEnter(() =>
                {
                    ChessConst.currentChessTeam = ChessTeam.Player1;
                    // Set ui color
                    SetTeamUIColor(ChessConst.currentChessTeam);
                    Debug.Log("Player1 round...");
                });

            // Third set the player2 who can put chess to be current player
            rounds.Config(Round.Player2)
                .AddTrigger(RoundTrigger.PlayerEnd, Round.Conclusion)
                .OnEnter(() =>
                {
                    ChessConst.currentChessTeam = ChessTeam.Player2;
                    // Set ui color
                    SetTeamUIColor(ChessConst.currentChessTeam);
                    Debug.Log("Player2 round...");
                });

            rounds.Trigger(RoundTrigger.None);
        }

        public void OldGenerateChessBoard(Vector2Int size)
        {
            // reset the team that put on chess board
            ChessConst.ResetTeam();
            // set the size/shape of the board that will generate
            boardSize = size;
            // instance the board
            if (chessBoard == null)
                chessBoard = new BaseChessBoard();
            // set the board shape/size
            chessBoard.InitBoard(boardSize);
            // Generate the father object
            boardGameObject = new GameObject("ChessBoard");
            // Calculate the origin positoin of the first board unit
            Vector2 originPosition = new Vector2(-boardSize.x + 1.0f, boardSize.y - 1.0f);
            Vector2 position = originPosition;

            for (int y = 0; y < boardSize.y; y++)
            {
                for (int x = 0; x < boardSize.x; x++)
                {
                    // Initialize the unit of board
                    GameObject gameObject = GameObject.Instantiate(unitBoardPrefabs);
                    gameObject.transform.position = position;
                    gameObject.transform.parent = boardGameObject.transform;
                    // Set the unit's event
                    BoardCoordinateFlag flag = gameObject.GetComponent<BoardCoordinateFlag>();
                    flag.infoPanel = infoPanel;
                    flag.coordinate = new Vector2Int(x, y);
                    flag.OnMouseClickEvent += (BaseChess chess) =>
                    {
                        this.PutChess(chess, flag.coordinate);
                    };

                    position.x += 2.0f;
                }
                position.y -= 2.0f;
                position.x = originPosition.x;
            }
        }

        public void ClearChessBoard()
        {
            Destroy(boardGameObject);
            boardGameObject = null;
        }

        public void PutChess(BaseChess chess, Vector2Int position)
        {
            chessBoard.PutChess(chess, position);
            rounds.Trigger(RoundTrigger.PlayerEnd);
        }

        public void SetTeamUIColor(ChessTeam team)
        {
            currentTeamIamge.color = team == ChessTeam.Player1 ? Color.white : Color.black;
        }
    }
}
